<template>
  <div>
    <el-button type="primary" size="mini" class="addBtn" @click="handlerAddLink">添加友链</el-button>
    <el-table
      v-loading="loading"
      :data="link"
      border
      style="width: 100%"
      element-loading-text="数据加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-table-column prop="linkId" label="ID" width="100" />
      <el-table-column prop="linkName" label="友链名称" />
      <el-table-column prop="linkUrl" label="友链链接" />
      <el-table-column prop="createdTime" label="创建时间" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handlerDelete(scope.row.linkId)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerEdit(scope.row.linkId)">更新</el-button>
              </el-dropdown-item>

            </el-dropdown-menu>
          </el-dropdown>
        </template>

      </el-table-column>
    </el-table>
    <el-drawer
      title="添加"
      :visible.sync="linkAddDialog"
    >
      <link-add @getLinkList="getLinkList" @closeLinkAddDialog="closeLinkAddDialog" />
    </el-drawer>
    <el-drawer
      title="更新"
      :visible.sync="linkUpdateDialog"
    >
      <link-update :link="link" @getLinkList="getLinkList" @closeLinkUpdateDialog="closeLinkUpdateDialog" />
    </el-drawer>
  </div>
</template>

<script>
import linkApi from '@/api/link'
import linkAdd from './link-add'
import linkUpdate from './link-update'
export default {
  name: 'LinkList',
  components: {
    linkAdd,
    linkUpdate
  },
  data() {
    return {
      // 数据列表
      drawer: false,
      loading: true,
      link: [],
      multipleSelection: [],
      linkAddDialog: false, // 添加音乐弹框
      linkUpdateDialog: false, // 更新
      readLrcVisible: false, // 读歌词
      lrc: '',
      linkUrl: '',
      loadLink: false
    }
  },
  watch: {
  },
  created() {
    this.getLinkList()
  },
  methods: {
    // 启用
    handlerEnable(id) {
      this.$confirm('此操作将启用该歌曲, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        linkApi.enableById(id).then(res => {
          this.$message.success(res.msg)
          linkApi.getByPage(this.page).then(res => {
            if (res.code === 200) {
              this.page = res.data
            } else {
              this.$message.error(res.msg)
            }
          })
        })
      })
    },
    getLinkList() {
      this.loading = true
      linkApi.getAll().then(res => {
        if (res.code === 200) {
          this.link = res.data
          this.loading = false
        } else {
          this.$message.error(res.msg)
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 添加歌曲
    handlerAddLink() {
      this.linkAddDialog = true
    },
    handlerEdit(id) {
      this.linkUpdateDialog = true
      linkApi.get(id).then(res => {
        if (res.code === 200) {
          this.link = res.data
          this.$message.success('成功加载信息')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handlerDelete(id) {
      this.$confirm('此操作将删除该歌曲, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        linkApi.deleteById(id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
          }
          linkApi.getAll().then(res => {
            if (res.code === 200) {
              this.link = res.data
            } else {
              this.$message.error(res.msg)
            }
          })
        })
      })
    },
    closeLinkAddDialog() {
      this.linkAddDialog = false
    },
    closeLinkUpdateDialog() {
      this.linkUpdateDialog = false
    }
  }
}
</script>
<style scoped lang="scss">
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .sp{
    font-size: 15px;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
